﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.Common;
using System.Text;
using He.Web.UI;

namespace He.Web.manage.links
{
    public partial class list : Manage
    {
        public int psize = 10;
        public int page;
        public int pcount;
        public string pos_mark;
        protected void Page_Load(object sender, EventArgs e)
        {
            pos_mark = GetString("pos_mark");
            page = GetInt("page", 0);
            if (!IsPostBack)
            {
                chkLoginLevel("view" + pos_mark);

                CookieHelper.Set("LastListUrl", Request.RawUrl);    //保存最后一次访问的列表页Url

                RptBind();
            }
        }

        #region 数据绑定
        private void RptBind()
        {
            BLL.links bll = new BLL.links();
            string strWhere = CombPlugSqlTxt(pos_mark, lang);
            rptList.DataSource = bll.GetPageList(psize, page, strWhere, "sort_id asc,id desc");
            rptList.DataBind();
            pcount = bll.GetRecordCount(strWhere);
        }
        #endregion

        #region 组合URL语句
        public string Comb_UrlTxt(string _lang, string _pos_mark)
        {
            StringBuilder strTemp = new StringBuilder();
            strTemp.AppendFormat("lang={0}&pos_mark={1}&", _lang, _pos_mark);
            return strTemp.ToString();
        }
        #endregion

        //推荐
        protected void ibtnRed_Command(object sender, CommandEventArgs e)
        {
            chkLoginLevel("edit" + pos_mark);
            int id = Convert.ToInt32(e.CommandArgument);
            BLL.links bll = new BLL.links();
            Model.links model = bll.GetModel(id);
            model.is_red = model.is_red == 1 ? 0 : 1;
            bll.Update(model);
            SaveLogs(string.Format("[友情链接管理]修改属性:{0}", model.title));
            Response.Redirect(Request.RawUrl);
        }

        //删除
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("del" + pos_mark);
            BLL.links bll = new BLL.links();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                if (cb.Checked)
                {
                    Model.links model = bll.GetModel(id);
                    FileHelper.DeleteFile(model.img_url);
                    bll.Delete(id);
                }
            }
            SaveLogs("[友情链接管理]批量删除");
            JscriptPrint("批量删除成功啦！", Request.RawUrl, "Success");
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            chkLoginLevel("edit" + pos_mark);
            BLL.links bll = new BLL.links();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            SaveLogs("[友情链接管理]批量保存排序");
            JscriptPrint("排序保存成功啦！", Request.RawUrl, "Success");
        }
    }
}
